package com.boc.langxiang.manager;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.util.Log;

import com.boc.langxiang.App;
import com.boc.langxiang.entity.bean.City;
import com.boc.langxiang.entity.bean.District;
import com.boc.langxiang.entity.bean.Province;
import com.boc.langxiang.entity.events.CityReadDoneEvent;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * CreateTime: 2016-05-23上午11:49
 * Author: wjkjinke00@126.com
 * Description:
 */
public class CityManeger {
    private CityManeger() {
    }

    public static ArrayList<Province> provinceList = new ArrayList<>();
    public static ArrayList<List<City>> cityList = new ArrayList<>();
    public static ArrayList<List<List<District>>> distirctList = new ArrayList<>();
    public static ArrayList<String> provinceNames = new ArrayList<>();
    public static ArrayList<List<String>> cityNames = new ArrayList<>();
    public static ArrayList<List<List<String>>> distirctNames = new ArrayList<>();

    public static void getCity() {
        if (provinceList == null || provinceList.isEmpty()
                || cityList == null || cityList.isEmpty()
                || distirctList == null || distirctList.isEmpty()) {
            ReadFileTask readFileTask = new ReadFileTask();
            readFileTask.execute();
        } else {
            EventBus.getDefault().post(new CityReadDoneEvent());
        }

    }

    static class ReadFileTask extends AsyncTask<Void, Object, Object> {
        @Override
        protected Object doInBackground(Void... params) {
            getLocalData(App.getInstance());
            Log.i("省份------------", "" + provinceList.size());
            Log.i("市区------------", "" + cityList.size());
            Log.i("县------------", "" + distirctList.size());

            EventBus.getDefault().post(new CityReadDoneEvent());
            return null;
        }
    }

    private static void getLocalData(Context context) {
        AssetManager assetManager = context.getAssets();
        try {
            InputStream inputStream = assetManager.open("district.json");
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            String json = new String(buffer, "utf-8");
            inputStream.close();
            ProvinceBean provinceBean = new Gson().fromJson(json, ProvinceBean.class);
            for (int i = 0; i < provinceBean.getCityBeanList().size(); i++) {
                CityBean cityBean = provinceBean.getCityBeanList().get(i);
                provinceList.add(cityBean.getProvince());
                provinceNames.add(cityBean.getProvince().getName());
                List<City> cityItem = new ArrayList<>();
                List<String> cityName = new ArrayList<>();
                List<List<District>> districts = new ArrayList<>();
                List<List<String>> districtItemName = new ArrayList<>();
                for (int j = 0; j < cityBean.getDistrictBeanList().size(); j++) {
                    cityItem.add(cityBean.getDistrictBeanList().get(j).getCity());
                    cityName.add(cityBean.getDistrictBeanList().get(j).getCity().getName());
                    DistrictBean districtBean = cityBean.getDistrictBeanList().get(j);
                    List<District> districtItem = new ArrayList<>();
                    List<String> districtName = new ArrayList<>();
                    for (int k = 0; k < districtBean.getDistrictList().size(); k++) {
                        districtItem.add(districtBean.getDistrictList().get(k));
                        districtName.add(districtBean.getDistrictList().get(k).getName());
                    }
                    districts.add(districtItem);
                    districtItemName.add(districtName);
                }
                distirctNames.add(districtItemName);
                cityNames.add(cityName);
                distirctList.add(districts);
                cityList.add(cityItem);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class DistrictBean {
        private City city;
        private List<District> districtList;

        public City getCity() {
            return city;
        }

        public void setCity(City city) {
            this.city = city;
        }

        public List<District> getDistrictList() {
            return districtList;
        }

        public void setDistrictList(List<District> districtList) {
            this.districtList = districtList;
        }
    }

    public static class CityBean {
        private Province province;
        private List<DistrictBean> districtBeanList;

        public Province getProvince() {
            return province;
        }

        public void setProvince(Province province) {
            this.province = province;
        }

        public List<DistrictBean> getDistrictBeanList() {
            return districtBeanList;
        }

        public void setDistrictBeanList(List<DistrictBean> districtBeanList) {
            this.districtBeanList = districtBeanList;
        }
    }

    public static class ProvinceBean {
        private List<CityBean> cityBeanList;

        public List<CityBean> getCityBeanList() {
            return cityBeanList;
        }

        public void setCityBeanList(List<CityBean> cityBeanList) {
            this.cityBeanList = cityBeanList;
        }
    }
}